package com.atguigu.exam.service.impl;

import com.atguigu.exam.entity.Banner;
import com.atguigu.exam.mapper.BannerMapper;
import com.atguigu.exam.service.BannerService;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.UpdateChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 轮播图服务实现类
 */
@Service
public class BannerServiceImpl extends ServiceImpl<BannerMapper, Banner> implements BannerService {
    @Autowired
    private BannerMapper bannerMapper;
    /**
     * 获取所有轮播图（管理后台使用）
     * @return 轮播图列表
     */
    @Override
    public List<Banner> getAllBanners() {
        LambdaQueryWrapper<Banner> lqw = new LambdaQueryWrapper<>();
        lqw.orderByAsc(Banner::getSortOrder);
        List<Banner> banners = this.list(lqw);
        return banners;
    }
    /**
     * 获取启用的轮播图（前台首页使用）
     * @return 轮播图列表
     */
    @Override
    public List<Banner> getActiveBanners() {
        LambdaQueryWrapper<Banner> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Banner::getIsActive, true);
        lqw.orderByAsc(Banner::getSortOrder);
        List<Banner> banners = this.list(lqw);
        return banners;
    }
    /**
     * 启用/禁用轮播图
     * @param id 轮播图ID
     * @param isActive 是否启用
     * @return 操作结果
     */
    @Override
    public boolean toggleBannerStatus(Long id, Boolean isActive) {
        LambdaUpdateWrapper<Banner> luw = new LambdaUpdateWrapper<>();
        luw.eq(Banner::getId, id);
        luw.set(Banner::getIsActive, isActive);
        return this.update(luw);
    }
    /**
     * 删除轮播图
     * @param id 轮播图ID
     * @return 操作结果
     */
    @Override
    public boolean deleteBanner(Long id) {
        return this.removeById(id);
    }
    /**
     * 根据ID获取轮播图
     * @param id 轮播图ID
     * @return 轮播图详情
     */
    @Override
    public Banner getBannerById(Long id) {
        return super.getById(id);
    }
}